1
Introduzione a PyTorch: perché i tensori contano
EvoClass-AI002Lecture 1
00:00

Introduzione a PyTorch: perché i tensori contano

PyTorch è un framework open source altamente flessibile e dinamico apprezzato per la ricerca in ambito deep learning e lo sviluppo rapido di prototipi. Al suo centro si trova il tensore che rappresenta la struttura dati fondamentale. Si tratta di un array multidimensionale progettato per gestire in modo efficiente le operazioni numeriche richieste dai modelli di deep learning, supportando automaticamente accelerazione GPU automaticamente.

1. Comprendere la struttura del tensore

Ogni input, output e parametro del modello in PyTorch è racchiuso in un tensore. Svolgono lo stesso ruolo degli array NumPy ma sono ottimizzati per l'elaborazione su hardware specializzato come GPU, rendendoli molto più efficienti per le operazioni di algebra lineare su larga scala richieste dai reti neurali.

Proprietà chiave che definiscono il tensore:

  • Forma: Definisce le dimensioni dei dati, espressa come una tupla (ad esempio, $4 \times 32 \times 32$ per un batch di immagini).
  • Tipo: Specifica il tipo numerico degli elementi memorizzati (ad esempio, torch.float32 per i pesi del modello, torch.int64 per l'indicizzazione).
  • Dispositivo: Indica la posizione fisica dell'hardware: tipicamente 'cpu' o 'cuda' (GPU NVIDIA).
Grafico dinamico e Autograd
PyTorch utilizza un modello di esecuzione imperativo, il che significa che il grafico computazionale viene costruito durante l'esecuzione delle operazioni. Questo permette al motore integrato di differenziazione automatica, Autograd, di tenere traccia di ogni operazione su un tensore, purché la proprietà requires_grad=True sia impostata, consentendo un calcolo semplice dei gradienti durante la retropropagazione.
fundamentals.py
TERMINALbash — pytorch-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live

Run code to inspect active tensors
Question 1
Which command creates a $5 \times 5$ tensor containing random numbers following a uniform distribution between 0 and 1?
torch.rand(5, 5)
torch.random(5, 5)
torch.uniform(5, 5)
torch.randn(5, 5)
Question 2
If tensor $A$ is on the CPU, and tensor $B$ is on the CUDA device, what happens if you try to compute $A + B$?
An error occurs because operations require tensors on the same device.
PyTorch automatically moves $A$ to the CUDA device and proceeds.
The operation is performed on the CPU, and the result is returned to the CPU.
Question 3
What is the most common data type (dtype) used for model weights and intermediate calculations in Deep Learning?
torch.float32 (single-precision floating point)
torch.int64 (long integer)
torch.bool
torch.float64 (double-precision floating point)
Challenge: Tensor Manipulation and Shape
Prepare a tensor for a specific matrix operation.
You have a feature vector $F$ of shape $(10,)$. You need to multiply it by a weight matrix $W$ of shape $(10, 5)$. For matrix multiplication (MatMul) to work, $F$ must be 2-dimensional.
Step 1
What should the shape of $F$ be before multiplication with $W$?
Solution:
The inner dimensions must match, so $F$ must be $(1, 10)$. Then $(1, 10) @ (10, 5) \rightarrow (1, 5)$.
Code: F_new = F.unsqueeze(0) or F_new = F.view(1, -1)
Step 2
Perform the matrix multiplication between $F_{new}$ and $W$ (shape $(10, 5)$).
Solution:
The operation is straightforward MatMul.
Code: output = F_new @ W or output = torch.matmul(F_new, W)
Step 3
Which method explicitly returns a tensor with the specified dimensions, allowing you to flatten the tensor back to $(50,)$? (Assume $F$ was $(5, 10)$ initially and is now flattened.)
Solution:
Use the view or reshape methods. The fastest way to flatten is often using -1 for one dimension.
Code: F_flat = F.view(-1) or F_flat = F.reshape(50)